package tech.ezrealc.fall2023.service.affair.classe;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.ezrealc.fall2023.exception.RelatedRecordException;
import tech.ezrealc.fall2023.model.affair.classe.ClasseDTO;
import tech.ezrealc.fall2023.model.affair.classe.mapper.ClasseMapper;
import tech.ezrealc.fall2023.repository.affair.classe.ClasseRepository;

import java.math.BigInteger;
import java.util.List;

@Service
@Transactional
public class ClasseService {

    @Resource
    private ClasseRepository classeRepository;
    @Resource
    private ClasseMapper classeMapper;

    public List<ClasseDTO> queryAllRecords() {
        return classeMapper.toDTOList(classeRepository.selectAll());
    }

    public void addRecord(ClasseDTO classeDTO) {
        classeRepository.insert(classeMapper.toDO(classeDTO));
    }

    public void deleteRecord(BigInteger id){
        Integer relatedRows = classeRepository.checkRelated(id);
        if (0 != relatedRows){
            throw new RelatedRecordException("Related Records: " + relatedRows);
        }else {
            classeRepository.delete(id);
        }
    }

    public void modifyRecord(ClasseDTO classeDTO) {
        classeRepository.update(classeMapper.toDO(classeDTO));
    }

}
